package com.chuanyang.driverh.mainhome;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.bean.OrderStatusEntity;
import com.chuanyang.driverh.mainhome.adapter.OrderStatusAdapter;
import com.chuanyang.driverh.responseresult.OrderDatailResult;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.responseresult.OrderMsgStatusResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.SimpleRatingBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class OrderStatusActivity extends BaseTitleActivity {

    private ImageView iv_order_msg_photo;
    private SimpleRatingBar rb_order_msg_star;
    private TextView tv_order_status;
    private TextView tv_order_num;
    private TextView tv_order_msg_phone;
    private TextView tv_status;
    private ListView lv_time_axle;
    private OrderStatusAdapter orderStatusAdapter;
    private List<OrderStatusEntity> orderStatusEntityList = new ArrayList<>();
    private String orderId;


    @Override
    protected int getContentResId() {
        return R.layout.activity_order_status;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBackImg();
        getTitleTextView().setText("状态");
        iv_order_msg_photo = (ImageView) findViewById(R.id.iv_order_msg_photo);
        rb_order_msg_star = (SimpleRatingBar) findViewById(R.id.rb_order_msg_star);
        tv_order_status = (TextView) findViewById(R.id.tv_order_status);
        tv_order_num = (TextView) findViewById(R.id.tv_order_num);
        tv_order_msg_phone = (TextView) findViewById(R.id.tv_order_msg_phone);
        tv_status = (TextView) findViewById(R.id.tv_status);
        lv_time_axle = (ListView) findViewById(R.id.lv_time_axle);

    }

    @Override
    public void initData() {
        if (!TextUtils.isEmpty(getIntent().getStringExtra("orderId"))) {
            orderId = getIntent().getStringExtra("orderId");
        }
        orderStatusAdapter = new OrderStatusAdapter(OrderStatusActivity.this, orderStatusEntityList);
        lv_time_axle.setAdapter(orderStatusAdapter);

        orderStatusMsg(orderId);
        getTopData(orderId);
    }



    @Override
    protected void initListener() {
    }

    private void orderStatusMsg(String orderId) {
        DialogUtil.showLoadingDialog(activityContext, "加载中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId + "");
        params.put("orderBy", "desc");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.checkOrderStatus(activityContext, params, new MyCallback<OrderMsgStatusResult>(activityContext, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(OrderMsgStatusResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    orderStatusEntityList.addAll(result.getData());
                    orderStatusAdapter.notifyDataSetChanged();
                } else {
                    PrintUtil.toast(activityContext, result.getMsg());
                }
            }
        });
    }
    private void getTopData(String orderId) {
        DialogUtil.showLoadingDialog(activityContext, "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("orderId", orderId);
        MyHttpUtil.selectOrderMsgById(activityContext, params, new MyCallback<OrderDatailResult>(activityContext, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(OrderDatailResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    OrderDetailData data = result.getData();
                    if (data != null) {
                        BitmapUtil.showImgForNetPath(data.getUserImage(), iv_order_msg_photo);
                        rb_order_msg_star.setRating(data.getStar());
                        tv_order_num.setText(data.getOrderNo());
                        tv_order_msg_phone.setText(data.getPhone());
                         setOrderStatus(result.getData().getOrder_status());

                    }
                } else {
                    PrintUtil.toast(activityContext, result.getMsg());
                }
            }
        });
    }

    private void setOrderStatus(int order_status) {
        if (order_status==0){
            tv_order_status.setText("初始化");
        }else if(order_status==1){
            tv_order_status.setText("待分配");
        }else if(order_status==2){
            tv_order_status.setText("待装货");
        }else if(order_status==3){
            tv_order_status.setText("装货中");
        }else if(order_status==4){
            tv_order_status.setText("运输中");
        }else if(order_status==5){
            tv_order_status.setText("卸货中");
        }else if(order_status==6){
            tv_order_status.setText("待确认收货");
        }else if(order_status==7){
            tv_order_status.setText("已完成");
        }else if(order_status==8){
            tv_order_status.setText("已取消");
        }



    }

}
